--[[
	SitRoleNode2.csb 是桌上普通玩家，  SitRoleNode.csb 是富豪或者赌神玩家界面
]]

--坐下玩家
local SitRoleNode = class("SitRoleNode", cc.Node)
local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local ClipText = appdf.req(appdf.EXTERNAL_SRC .. "ClipText")
local PopupInfoHead = appdf.req(appdf.EXTERNAL_SRC .. "PopupInfoHead")

function SitRoleNode:ctor( viewParent, index )
	self.m_parent = viewParent
	--加载csb资源
	local csbNode = ExternalFun.loadCSB("GamePublic/SitRoleNode.csb", self)
	self.m_csbNode = csbNode
	--背景特效
	
	--信息背景
	local infoBg = csbNode:getChildByName("player_info")
	self.infoBg = infoBg
	infoBg:setLocalZOrder(1)
	local player_btn = infoBg:getChildByName("player_btn")
	player_btn:addTouchEventListener(function() self:btcallback() end)
	--头像
	self.head_bg = infoBg:getChildByName("head_bg")
	self.head_bg:setTexture("public/default_head.png")
	self.head_size = self.head_bg:getContentSize()
	
	--金币
	self.m_textScore = infoBg:getChildByName("score_text")
	self.m_textScore:setLocalZOrder(1)

	--名字
	tmp = infoBg:getChildByName("name_text")
	self.m_clipNick = cc.Label:createWithSystemFont("昵称", "", 20)
	-- self.m_clipNick = cc.Label:createWithTTF("昵称", "fonts/round_body.ttf", 20)
	local px,py = tmp:getPosition()
	self.m_clipNick:setPosition(cc.p(px,py))
	infoBg:addChild(self.m_clipNick)
	self.m_clipNick:setLocalZOrder(1)
	self.m_clipNick:setAnchorPoint(cc.p(0.5,0.5))

	-- --分数
	self.m_nIndex = index
	self.m_spHead = nil

	--下注分数
	self.m_lJettonScore = {0,0,0,0}
	--飞行动画


	if index == 2 then
		self.m_frameAnimate = ccs.Armature:create("bairenniuniu_touxiangteiao")
		self.infoBg:addChild(self.m_frameAnimate)
		self.m_frameAnimate:setAnchorPoint(cc.p(0.5,0.5))
		self.m_frameAnimate:setPosition(cc.p(117/2 - 7,163/2 + 20))
		self.m_frameAnimate:getAnimation():play("Animation2_2",-1,1)	

		self.m_hganimate = ccs.Armature:create("bairenniuniu_touxiangteiao")
		self.infoBg:addChild(self.m_hganimate)
		self.m_hganimate:setAnchorPoint(cc.p(0.5,0.5))
		self.m_hganimate:setPosition(cc.p(117/2 - 7,163/2 + 20))
		self.m_hganimate:getAnimation():play("Animation2_1",-1,1)

	elseif index == 1 then
		self.m_frameAnimate = ccs.Armature:create("bairenniuniu_touxiangteiao")
		self.infoBg:addChild(self.m_frameAnimate)
		self.m_frameAnimate:setAnchorPoint(cc.p(0.5,0.5))
		self.m_frameAnimate:setPosition(cc.p(117/2 - 7,163/2 + 20))
		self.m_frameAnimate:getAnimation():play("Animation3_2",-1,1)	

		self.m_dsanimate = ccs.Armature:create("bairenniuniu_touxiangteiao")
		self.infoBg:addChild(self.m_dsanimate)
		self.m_dsanimate:setAnchorPoint(cc.p(0.5,0.5))
		self.m_dsanimate:setPosition(cc.p(117/2 - 7,163/2 + 20))
		self.m_dsanimate:getAnimation():play("Animation3_1",-1,1)
	end

	self.m_winEffect = ccs.Armature:create("bairenniuniu_touxiangteiao")
		self.infoBg:addChild(self.m_winEffect)
		self.m_winEffect:setAnchorPoint(cc.p(0.5,0.5))
		self.m_winEffect:setPosition(cc.p(117/2 - 7,163/2 + 20))
		
		self.m_starEffect = ccs.Armature:create("bairenniuniu_touxiangteiao")
		self.infoBg:addChild(self.m_starEffect)
		self.m_starEffect:setAnchorPoint(cc.p(0.5,0.5))
		self.m_starEffect:setPosition(cc.p(117/2 - 7,163/2 + 20))

		self.m_winEffect:setVisible(false)
		self.m_starEffect:setVisible(false)

		-- self.m_winEffect:getAnimation():play("Animation1_2")
		-- self.m_starEffect:getAnimation():play("Animation1_1")
end

function SitRoleNode:btcallback()
	
end

function SitRoleNode:onSitDown( useritem, bAni )
	if nil == useritem then
		return
	end
	self.m_wChair = useritem.chair_id
	self.m_szNickName = useritem.player_name
	self.m_lJettonScore = {0,0,0,0}
	--坐下特效
	if bAni then
		local act = cc.Repeat:create(cc.Blink:create(1.0,1),5)
		self.m_spEffect:stopAllActions()
		self.m_spEffect:runAction(act)
	end	

	--头像
	if useritem.player_head < 0 or useritem.player_head > 9 then
		useritem.player_head = 0
	end
	local str = string.format("Avatar%d.png", useritem.player_head)
	local frame = cc.SpriteFrameCache:getInstance():getSpriteFrame(str)
	if nil ~= frame then
		self.head_bg:setSpriteFrame(frame)
		local size = self.head_bg:getContentSize()	
		--self.head_bg:setScale(self.head_size.width/size.width+0.1)
	end
	--昵称
	ExternalFun.transName( useritem.player_name,self.m_clipNick)
	-- self.m_clipNick:setString(useritem.player_name)
	--金币
	local str = ExternalFun.ChangeScore(useritem.player_gold,2,"",false,false)
	if string.len(str) > 18 then
		str = string.sub(str,1,11) .. "..."
	end
	self.m_textScore:setString(str)
	local px,py = self.m_textScore:getPosition()
end

function SitRoleNode:getChair(  )
	return self.m_wChair
end

function SitRoleNode:getNickName( )
	return self.m_szNickName
end

--获取坐下位置
function SitRoleNode:getIndex( )
	return self.m_nIndex
end

--清空下注分数
function SitRoleNode:clearJettonScore()
	self.m_lJettonScore = {0,0,0,0}
end

function SitRoleNode:getJettonScoreWithArea(cbArea)
	return self.m_lJettonScore[cbArea]
end

function SitRoleNode:getJettonScore()
	return self.m_lJettonScore
end

--是否庄家
function SitRoleNode:updateBanker( isBanker )
	--庄家
	self.m_spBanker:setVisible(isBanker)
end

--金币动画、更新自己金币
function SitRoleNode:updateScore( lScore )
	if nil == lScore then
		return
	end
	--金币
	local str = ExternalFun.ChangeScore(lScore,2,"",false,false)
	if string.len(str) > 18 then
		str = string.sub(str,1,11) .. "..."
	end
	self.m_textScore:setString(str)
end

function SitRoleNode:shakechair(chairid)
	if self.inrunning then
		return
	end
	local ts = 0.05
	local moveBy1 = cc.MoveBy:create(ts, cc.p(30, 0))
	local moveBy2 = cc.MoveBy:create(ts, cc.p(-30, 0))
	if chairid > 3 then
		local tmp = moveBy1
		moveBy1 = moveBy2
		moveBy2 = tmp
	end
	self.inrunning = true
	self:runAction(cc.Sequence:create(
		moveBy1,moveBy2,cc.CallFunc:create(function( ... )
			self.inrunning = false
		end)
		)
	)
end

function SitRoleNode:gameEndScoreChange( useritem, changescore )
	self:updateScore(useritem.lScore)
	if 0 == changescore or not self.m_wChair then
		return
	end

	local str = "/" .. ExternalFun.ChangeScore(math.abs(changescore), 2,"",false,false).. ":"
	local fontstr = ""
	local Imgname = nil
	if changescore > 0 then
		fontstr = "image/ui_win_count.png" 
		Imgname= "image/shuzi3.png"

	elseif changescore < 0 then
		fontstr = "image/ui_lose_count.png"
		Imgname= "image/shuzi4.png"
	end

	
	
	self.bgImg = ccui.ImageView:create(Imgname,ccui.TextureResType.localType)
	
	self.m_atlasScore = cc.LabelAtlas:create(str,fontstr,26, 39, string.byte("."))
	self.bgImg:addChild(self.m_atlasScore)
	self.m_atlasScore:setAnchorPoint(cc.p(0.5,0.5))
	local bgsize = self.bgImg:getContentSize()
	self.m_atlasScore:setPosition(cc.p(bgsize.width/2,bgsize.height/2))
	self.infoBg:addChild(self.bgImg)
	
	self.m_atlasScore:setOpacity(255)
	self.bgImg:setOpacity(255)
	
	local offsetx = 50
	if self.m_wChair%2 == 0 then
		offsetx = 0
	end

	local call = cc.CallFunc:create(function( )

		if self.bgImg then
			self.bgImg:removeSelf()
		end
		if not tolua.isnull(self.m_atlasScore) then
			self.m_atlasScore:removeFromParent()
			self.m_atlasScore = nil
		end
		if not tolua.isnull(self.m_winEffect)  then
			self.m_winEffect:getAnimation():gotoAndPause(1)
			self.m_winEffect:setVisible(false)
		end
		if not tolua.isnull(self.m_starEffect)  then
			self.m_starEffect:getAnimation():gotoAndPause(1)
			self.m_starEffect:setVisible(false)
		end
	end)

	local moveBy = cc.MoveBy:create(0.8, cc.p(0, 55))
	local standby = cc.DelayTime:create(1)
	local fadeout = cc.FadeOut:create(1)

	local m_actShowScore = cc.Sequence:create(moveBy,standby ,fadeout, call)
	self.m_atlasScore:setScale(0.8)
	--self.m_atlasScore:setPosition(cc.p(0,70))
	local px1,py1 = self.bgImg:getPosition()
	self.bgImg:setPosition(px1 + 40,py1 + 110)
	self.bgImg:stopAllActions()
	self.bgImg:runAction(m_actShowScore)

	local act1 = cc.DelayTime:create(2.5)
	local fo = cc.FadeOut:create(1.9)
	local removeact = cc.RemoveSelf:create()
	local seqforatlas = cc.Sequence:create(act1,fo,removeact)
	self.m_atlasScore:runAction(seqforatlas)
	if changescore > 0 then
	self.m_winEffect:setVisible(true)
	self.m_starEffect:setVisible(true)
	end
	self.m_winEffect:getAnimation():play("Animation1_2")
	self.m_starEffect:getAnimation():play("Animation1_1")

end

function SitRoleNode:reSet(  )
	self.m_textScore:setString("")
	self.m_clipNick:setString("")
	self.head_bg:setTexture("public/default_head.png")
	self.m_wChair = nil
end

return SitRoleNode